package 题目集.链表.环;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * https://www.acwing.com/problem/content/1457/
 * 约瑟夫环
 */
public class 招聘 {
    public static void main(String[] args) {
        Reader sc = new Reader();
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            Queue<Integer> ms = new ArrayDeque<>();
            for (int i = 0; i < m; i++) {
                ms.add(sc.nextInt());
            }
            Queue<Integer> queue = new ArrayDeque<>();
            for (int i = 0; i < n; i++) {
                queue.add(i);
            }
            for (int i = 1; i < n; i++) {
                Integer poll = ms.poll();
                ms.add(poll);
                poll = (poll % queue.size()) - 1;
                if (poll < 0) {
                    poll = queue.size()+poll;
                }
                for (int j = 0; j < poll; j++) {
                    queue.add(queue.poll());
                }
                queue.poll();
            }
            System.out.println(queue.poll());
        }
    }
    static class Reader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = new StringTokenizer("");

        private String innerNextLine() {
            try {
                return reader.readLine();
            } catch (IOException ex) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!tokenizer.hasMoreTokens()) {    //内存中没有
                String nextLine = innerNextLine();
                if (nextLine == null) {     //且没有下一行
                    return false;
                }
                tokenizer = new StringTokenizer(nextLine);  //读入一行
            }
            return true;
        }

        public String nextLine() {
            tokenizer = new StringTokenizer("");    //清空内存中的字符
            return innerNextLine();
        }
        public void clear(){
            tokenizer = new StringTokenizer("");
        }

        public String next() {
            hasNext();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble(){
            return Double.parseDouble(next());
        }
//    public BigInteger nextBigInt() {
//        return new BigInteger(next());
//    }
    }

}
